package com.hualan.sfzy.task;

import com.hualan.sfzy.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
public class ArticleRecycleScheduler {

    @Autowired
    private ArticleMapper articleMapper;

    // 每天凌晨 2 点执行一次
    @Scheduled(cron = "0 0 2 * * ?")
    public void clearExpiredArticlesFromRecycleBin() {
        int days = 15; // 清除超过 15 天的文章
        int deleted = articleMapper.deleteExpiredArticles(days);
        
        String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println("[" + currentTime + "] 定时清除回收站中过期文章数量: " + deleted + " 条");
        
        if (deleted > 0) {
            System.out.println("[" + currentTime + "] 已清理超过 " + days + " 天的回收站文章");
        }
    }
}